export const ACTION_SET_FROM = 'SET_FROM'
export const ACTION_SET_TO = 'SET_TO'
export const ACTION_SET_ISCITYMODALOPEN = 'SET_ISCITYMODALOPEN'
export const ACTION_SET_SELECTLEFTCITY = 'SET_SELECTLEFTCITY'
export const ACTION_SET_CITYDATA = 'SET_CITYDATA'
export const ACTION_SET_ISLOADINGCITYDATA = 'SET_ISLOADINGCITYDATA'
export const ACTION_SET_ISDATEMODALOPEN = 'SET_ISDATEMODALOPEN'
export const ACTION_SET_HIGHSPEED = 'SET_HIGHSPEED'
export const ACTION_SET_DEPARTDATE = 'SET_DEPARTDATE'

export function setFrom (from) {
  return {
    type: ACTION_SET_FROM,
    payload: from 
  }
}
export function setTo (to) {
  return {
    type: ACTION_SET_TO,
    payload: to
  }
}
export function setIsCityModalOpen (isCityModalOpen) {
  return {
    type: ACTION_SET_ISCITYMODALOPEN,
    payload: isCityModalOpen
  }
}
export function setSelectLeftCity (selectLeftCity) {
  return {
    type: ACTION_SET_SELECTLEFTCITY,
    payload: selectLeftCity
  }
}
export function clickShowCity (selectLeftCity) {
  return (dispatch) => {
    dispatch({
      type: ACTION_SET_ISCITYMODALOPEN,
      payload: true
    })
    dispatch({
      type: ACTION_SET_SELECTLEFTCITY,
      payload: selectLeftCity
    })
  }
}

export function clickHideCity () {
  return {
    type: ACTION_SET_ISCITYMODALOPEN,
    payload: false
  }
}

export function setSelectedCity (city) {
  return (dispatch, getState) => {
    const { selectLeftCity } = getState()
    
    if(selectLeftCity) {
      dispatch(setFrom(city))
    }else{
      dispatch(setTo(city))
    }
    dispatch(clickHideCity())
  }
}



export function setCityData (cityData) {
  return {
    type: ACTION_SET_CITYDATA,
    payload: cityData
  }
}
export function setIsLoadingCityData (isLoadingCityData) {
  return {
    type: ACTION_SET_ISLOADINGCITYDATA,
    payload: isLoadingCityData
  }
}
export function setIsDateModalOpen (isDateModalOpen) {
  return {
    type: ACTION_SET_ISDATEMODALOPEN,
    payload: isDateModalOpen
  }
}
export function toggleHighSpeed (highSpeed) {
  return (dispatch, getState) => {
    const {highSpeed} = getState();
    dispatch({
      type: ACTION_SET_HIGHSPEED,
      payload: !highSpeed
    })
  }
}

export function showDateModel () {
  return {
    type: ACTION_SET_ISDATEMODALOPEN,
    payload: true
  }
}
export function hideDateModel () {
  return {
    type: ACTION_SET_ISDATEMODALOPEN,
    payload: false
  }
}

export function exchangeFromTo() {
  return (dispatch, getState) => {
    const {from, to} = getState()
    dispatch(setFrom(to))
    dispatch(setTo(from))
  }
}

export function setDepartDate (date) {
  return {
    type: ACTION_SET_DEPARTDATE,
    payload: date
  }
}

export function fetchCityData () {
  return (dispatch, getState) => {
    const {isLoadingCityData} = getState()
    if(isLoadingCityData) return
    const cache = JSON.parse(localStorage.getItem('city_data_cache') || '{}')
    if(Date.now() < cache.expires) {
      dispatch(setCityData(cache.data))
      return
    }
    

    dispatch(setIsLoadingCityData(true))
    fetch('http://localhost:8089/rest/cities?_' + Date.now())
      .then(res => res.json())
      .then(cityData => {
        dispatch(setCityData(cityData))
        localStorage.setItem('city_data_cache', JSON.stringify({
          expires: Date.now() + 60 * 1000,
          data: cityData
        }))
        dispatch(setIsLoadingCityData(false))
      })
      .catch(err => {
        dispatch(setIsLoadingCityData(false))
      })
  }
}